package socket;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Scanner;

public class Client {
    private Socket socket;
    public  Client(){
        try {
            //System.out.println("正在连接服务端");
            socket = new Socket("localhost", 8088);
            //socket = new Socket("176.61.40.18", 8088);
            System.out.println("连接成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void start(){
            try {
                OutputStream out = socket.getOutputStream();
                Scanner sc = new Scanner(System.in);
                while (true) {
                    System.out.println("请输入字符串：");
                    String line = sc.nextLine();
                    if(line.equals("exit")) {
                        break;
                    }
                    byte[] datas = line.getBytes(StandardCharsets.UTF_8);
                    out.write(datas.length);
                    out.write(datas);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                    try {
                        if(socket != null) {
                            socket.close();
                        }
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
            }
    }

    public static void main(String[] args) {
        Client client = new Client();
        client.start();
    }
}
